package org.wishwork.api.allocate.upload.kc;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wishwork.api.Api;
import org.wishwork.api.allocate.upload.in.request.Item;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 库存调拨上传（跨仓调拨）
 * @author: WangYX
 * @create: 2022-04-12 22:18
 * @Version: 1.0.0
 **/

public class AllocateKcUploadApi extends Api<List<AllocateKcUploadResponse>, AllocateKcUploadRequest> {

    private static final Logger log = LoggerFactory.getLogger(AllocateKcUploadApi.class);

    private Context context;

    public AllocateKcUploadApi(Context context) {
        this.context = context;
    }


    @Override
    protected String query(AllocateKcUploadRequest allocateKcUploadRequest) throws Exception {
        String url = "/open/allocate/kc/upload";
        Map<String, Object> params = new HashMap<>();
        params.put("so_id", allocateKcUploadRequest.getIoDate());
        params.put("io_date", allocateKcUploadRequest.getIoDate());
        if (ObjectUtil.isNotEmpty(allocateKcUploadRequest.getPrice())) {
            params.put("price", allocateKcUploadRequest.getPrice());
        }
        params.put("go_co_id", allocateKcUploadRequest.getGoCoId());
        if (allocateKcUploadRequest.getWarehouse() != null) {
            params.put("warehouse", allocateKcUploadRequest.getWarehouse());
        }
        params.put("link_co_id", allocateKcUploadRequest.getLinkCoId());
        if (allocateKcUploadRequest.getLinkWarehouse() != null) {
            params.put("link_warehouse", allocateKcUploadRequest.getLinkWarehouse());
        }
        if (StrUtil.isNotEmpty(allocateKcUploadRequest.getRemark())) {
            params.put("remark", allocateKcUploadRequest.getRemark());
        }
        if (allocateKcUploadRequest.getItems() != null && allocateKcUploadRequest.getItems().size() > 0) {
            List<Map<String, Object>> list = new ArrayList<>();
            for (Item item : allocateKcUploadRequest.getItems()) {
                Map<String, Object> map = new HashMap<>();
                map.put("qty", item.getQty());
                map.put("sku_id", item.getSkuId());
                if (StrUtil.isNotEmpty(item.getBatchId())) {
                    map.put("batch_id", item.getBatchId());
                }
                if (StrUtil.isNotEmpty(item.getProducedDate())) {
                    map.put("produced_date", item.getProducedDate());
                }
                if (StrUtil.isNotEmpty(item.getExpirationDate())) {
                    map.put("expiration_date", item.getExpirationDate());
                }
                list.add(map);
            }
            params.put("items", list);
        }
        if (StrUtil.isNotEmpty(allocateKcUploadRequest.getReceiverName())) {
            params.put("receiver_name", allocateKcUploadRequest.getReceiverName());
        }
        if (StrUtil.isNotEmpty(allocateKcUploadRequest.getReceiverMobile())) {
            params.put("receiver_mobile", allocateKcUploadRequest.getReceiverMobile());
        }
        if (StrUtil.isNotEmpty(allocateKcUploadRequest.getReceiverState())) {
            params.put("receiver_state", allocateKcUploadRequest.getReceiverState());
        }
        if (StrUtil.isNotEmpty(allocateKcUploadRequest.getReceiverCity())) {
            params.put("receiver_city", allocateKcUploadRequest.getReceiverCity());
        }
        if (StrUtil.isNotEmpty(allocateKcUploadRequest.getReceiverDistrict())) {
            params.put("receiver_district", allocateKcUploadRequest.getReceiverDistrict());
        }
        if (StrUtil.isNotEmpty(allocateKcUploadRequest.getReceiverAddress())) {
            params.put("receiver_address", allocateKcUploadRequest.getReceiverAddress());
        }
        return HttpHelper.sendRequest(params, url);
    }

    @Override
    protected List<AllocateKcUploadResponse> process(JSONObject data, AllocateKcUploadRequest allocateKcUploadRequest) throws Exception {
        JSONArray datas = data.getJSONArray("datas");
        List<AllocateKcUploadResponse> allocateKcUploadResponses = datas.toJavaList(AllocateKcUploadResponse.class);
        return allocateKcUploadResponses;
    }
}
